package com.ys.otms.services.impl;

import com.ys.otms.dao.IFindShipFeedbackDao;
import com.ys.otms.dto.FindShipFeedbackDto;
import com.ys.otms.dto.ParamDto;
import com.ys.otms.dto.ShipInfoDto;
import com.ys.otms.dto.UserShipDto;
import com.ys.otms.services.IFindShipFeedbackServices;
import com.ys.otms.utils.DateUtil;
import com.ys.otms.utils.JsonUtil;
import com.ys.otms.utils.PageUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.*;

/**
 * Created by Administrator on 2018/11/19.
 */
@Service
public class FindShipFeedbackServiceImpl implements IFindShipFeedbackServices {
    private static Logger logger = LoggerFactory.getLogger(FindShipFeedbackServiceImpl.class);

    @Autowired
    private IFindShipFeedbackDao iFindShipFeedbackDao;
    @Override
    public JSONObject selectShipFeedbackByPhone(HttpServletRequest request,JSONObject obj) throws Exception {
        logger.info("FindShipFeedbackServiceImpl selectShipFeedbackByPhone start obj{} ",obj);
        JSONArray jsonArray = JsonUtil.getJSONArray(obj,"shipList");
        JSONObject jsonObject = new JSONObject();
        List<FindShipFeedbackDto> findShipFeedbackDtoList = new ArrayList<>();
        FindShipFeedbackDto findShipFeedbackDto = null;
        if (jsonArray.size()>0){
            for (int i = 0; i <jsonArray.size() ; i++) {
                findShipFeedbackDto = new FindShipFeedbackDto();
                findShipFeedbackDto.setShipId(Integer.parseInt(jsonArray.getJSONObject(i).getString("shipId")));
                findShipFeedbackDto.setCompanyId(Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID")));
                findShipFeedbackDto.setFeedbackTime(DateUtil.getSeePhoneTime(new Timestamp((new Date()).getTime()),10));
                findShipFeedbackDtoList.add(findShipFeedbackDto);
            }
            List<FindShipFeedbackDto> findShipFeedbackDtoList1 = new ArrayList<>();
            List<FindShipFeedbackDto> findShipFeedbackDtos = iFindShipFeedbackDao.selectShipFeedbackByShipId(findShipFeedbackDtoList);
            if (findShipFeedbackDtos.size()>0){
                for (int i = 0; i < findShipFeedbackDtoList.size(); i++) {
                    for (int j = 0; j <findShipFeedbackDtos.size() ; j++) {
                        if (findShipFeedbackDtoList.get(i).getShipId().equals(findShipFeedbackDtos.get(j).getShipId()) && findShipFeedbackDtoList.get(i).getCompanyId().equals(findShipFeedbackDtos.get(j).getCompanyId())){
                            break;
                        }
                        if (j==findShipFeedbackDtos.size()-1){
                            findShipFeedbackDto = new FindShipFeedbackDto();
                            findShipFeedbackDto.setCompanyId(findShipFeedbackDtoList.get(i).getCompanyId());
                            findShipFeedbackDto.setFeedbackTime(findShipFeedbackDtoList.get(i).getFeedbackTime());
                            findShipFeedbackDto.setShipId(findShipFeedbackDtoList.get(i).getShipId());
                            findShipFeedbackDtoList1.add(findShipFeedbackDto);
                        }
                    }
                }
                for (int i = 0; i <findShipFeedbackDtos.size() ; i++) {
                    findShipFeedbackDto = new FindShipFeedbackDto();
                    findShipFeedbackDto.setShipId(findShipFeedbackDtos.get(i).getShipId());
                    findShipFeedbackDto.setFeedbackTime(DateUtil.getSeePhoneTime(new Timestamp((new Date()).getTime()),10));
                    iFindShipFeedbackDao.updateFindShipFeedbackByFeedbackTime(findShipFeedbackDto);
                }
                if (findShipFeedbackDtoList1.size()>0){
                    iFindShipFeedbackDao.insertFindShipFeedback(findShipFeedbackDtoList1);
                }
            }else{
                iFindShipFeedbackDao.insertFindShipFeedback(findShipFeedbackDtoList);
            }
            List<ShipInfoDto> shipInfoDtos = iFindShipFeedbackDao.selectShipInfo(findShipFeedbackDtoList);
            jsonObject.put("shipInfoDtos",shipInfoDtos);
            jsonObject.put("SUCCESS",true);
        }else{
            jsonObject.put("SUCCESS",false);
            jsonObject.put("MSG","请选择船舶！");
            return jsonObject;
        }
        return jsonObject;
    }

    @Override
    public Map<String,Object> selectShipFeedBack(HttpServletRequest request, ParamDto paramDto) throws Exception {
        logger.info("FindShipFeedbackServiceImpl selectShipFeedBack start obj{}",paramDto);
        paramDto.setCompanyId(Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID")));
        paramDto.setSeePhoneTime(new Timestamp((new Date()).getTime()));
        List<ShipInfoDto> shipInfoDtos = iFindShipFeedbackDao.selectFeedbackShipInfo(paramDto);
        for (int i = 0; i < shipInfoDtos.size(); i++) {
            shipInfoDtos.get(i).setFeedbackDate(DateUtil.getSeePhoneStr(shipInfoDtos.get(i).getFeedbackTime(),-10).toString().substring(0,DateUtil.getSeePhoneStr(shipInfoDtos.get(i).getFeedbackTime(),-10).toString().indexOf(".")));
        }
        int feedbackCount = iFindShipFeedbackDao.selectFeedbackShipInfoByCount(paramDto);
        Map<String,Object> map = new HashMap<>();
        map.put("data",shipInfoDtos);
        map.put("count",feedbackCount);
        return map;
    }
}
